home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / games2 / rotise12.zip / GETOPT.C < prev    next >
C/C++ Source or Header  |  1992-05-09  |  2KB  |  91 lines

  1. /* getopt.c -- A simple implementation of getopt()
  2.  
  3.     1989    Mark Mallett
  4.  
  5.     This is an implementation of getopt(3) for environments that
  6.     don't have one.  It may not be -- in fact, probably isn't --
  7.     fully compatible with the normal getopt(3), but it can be
  8.     used in a pinch, which is what this is.
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13.  
  14. #include "rotise.h"
  15.  
  16.     char    *optarg = NULL;    /* Option argument */
  17.     int    opterr = 1;    /* Whether to print errors. */
  18.     int    optind = 1;    /* Current option index */
  19. /*
  20.  
  21. *//* getopt( argc, argv, optP )
  22.  
  23.     Extract an option from a command line
  24.  
  25. Accepts :
  26.  
  27.     argc        Number of command line arguments
  28.     argv        Argument vector
  29.     optP        Ptr to option list, as in getopt(3);
  30.  
  31. Returns :
  32.  
  33.     <value>        EOF if no more options,
  34.             or the option character;
  35.  
  36. */
  37.  
  38. int getopt ARGLIST( (argc, argv, optP) )
  39.     NFARG( int        argc)        /* Argument count */
  40.     NFARG( char        **argv)        /* Ptrs to tokens */
  41.     FARG( char        *optP)        /* Ptr to option list */
  42. {
  43.     char        ch;        /* Char */
  44.  
  45.     /* If no more tokens, return EOF */
  46.     if ( optind >= argc )
  47.         return( EOF );
  48.  
  49.     /* Check first arg for switch (add '/' as switch for IBM'ers) */
  50.     if ( argv[optind][0] != '-' && argv[optind][0] != '/' )
  51.         return( EOF );
  52.  
  53.     /* Got option, probably; handle it. */
  54.     ch = argv[optind][1];
  55.     for( ;*optP != '\0'; ++optP ) {
  56.     if ( ch == *optP )
  57.         break;
  58.     if ( optP[1] == ':' )
  59.         ++optP;
  60.     }
  61.  
  62.     if ( *optP == '\0' ) {
  63.     /* Unwanted option character. */
  64.     if ( opterr )
  65.         fprintf( stderr, "%s: illegal option -- %c\n",
  66.         ( argv[0] != NULL ) && ( *argv[0] != '\0' ) ?
  67.             argv[0] : "WRAP", ch );
  68.     return( '?' );
  69.     }
  70.  
  71.     /* Check for argument wanted */
  72.     if ( optP[1] == ':' ) {
  73.     if ( argv[optind][2] != NUL )
  74.         optarg = &argv[optind][2];
  75.     else if ( optind < argc -1 )
  76.         optarg = argv[++optind];
  77.     else {
  78.         if ( opterr )
  79.         fprintf( stderr, "%s: option requires an argument -- %c\n",
  80.             ( argv[0] != NULL ) && ( *argv[0] != '\0' ) ?
  81.                 argv[0] : "<progname>", ch );
  82.         return( '?' );
  83.     }
  84.     }
  85.     else
  86.     optarg = NULL;
  87.  
  88.     ++optind;
  89.     return( ch );
  90. }
  91.